import os
import sys
import platform
import configparser

from native import MyTemplateNative
from function import injectFunction

# project path
projectHome = None
if platform.system() == "Windows":
    print("Windows")
    projectHome = os.path.dirname(os.path.abspath(__file__)).replace("\python", "")
elif platform.system() == "Linux":
    print("Linux")
    projectHome = os.path.dirname(os.path.abspath(__file__)).replace("/python", "")
elif platform.system() == "Darwin":
    print("Darwin")
    projectHome = os.path.dirname(os.path.abspath(__file__)).replace("/python", "")
else:
    print("Cannot supported OS.")
    sys.exit(1)

# project name
projectName = "testProjectName"
# # get Cargo.toml file path
cargoPath = os.path.join(projectHome, "Cargo.toml")
# # check file existed
if not os.path.exists(cargoPath):
    raise FileNotFoundError(f"Cargo.toml not found at {cargoPath}")

# # get project name
cargo = configparser.ConfigParser()
cargo.read(cargoPath)
projectName = str(cargo.get("package", "name")).replace("\"", "")

# dylib
dylibPath = None
if platform.system() == "Windows":
    dylibPath = projectHome + "/target/debug/" + projectName + ".dll"
elif platform.system() == "Linux":
    dylibPath = projectHome + "/target/debug/" + projectName + ".so"
elif platform.system() == "Darwin":
    dylibPath = projectHome + "/target/debug/lib" + projectName + ".dylib"
else:
    print("Cannot supported OS.")
    sys.exit(1)
# todo need project name

print("[dylibPath]:" + dylibPath)

native = MyTemplateNative(dylibPath)

basic_integer_result = native.basic_integer(5)
print("method basic_integer value is 5, result is " + str(basic_integer_result) + " typeof " + str(type(basic_integer_result)))

basic_uintger_result = native.basic_uinteger(5)
print("method basic_uinteger value is 5, result is " + str(basic_uintger_result) + " typeof " + str(type(basic_uintger_result)))

basic_float_result = native.basic_float(0.1)
print("method basic_float value is 0.1, result is " + str(basic_float_result) + " typeof " + str(type(basic_float_result)))

basic_double_result = native.basic_doube(0.1)
print("method basic_doube value is 0.1, result is " + str(basic_double_result) + " typeof " + str(type(basic_double_result)))

basic_bool_result = native.basic_bool(True)
print("method basic_bool value is True, result is " + str(basic_bool_result) + " typeof " + str(type(basic_bool_result)))

basic_str_result = native.basic_str("123")
print("method basic_str value is 123, result is " + str(basic_str_result) + " typeof " + str(type(basic_str_result)))

function = injectFunction()
native.inject_function(function.fn)

# compound type
array_12345_result = native.array_12345()
print("method array_12345 value is null, result is" + str(array_12345_result) + " typeof " + str(type(array_12345_result)))

array_months_result = native.array_months()
print("method array_months value is null, result is" + str(array_months_result) + " typeof " + str(type(array_months_result)))